//
//  FeedViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/3/5.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "FeedViewController.h"
#import "FeedCell.h"
#import "FeedModel.h"
//#import "UITableView+FDTemplateLayoutCell.h"
#import "LLZStatusBarAlertView.h"
#import "VisitViewController.h"
#import "XLPhotoBrowser.h"
#import "OneFeedViewController.h"

@interface FeedViewController ()<UITableViewDelegate, UITableViewDataSource, FeedCellDelegate, XLPhotoBrowserDelegate>

@end

@implementation FeedViewController

#pragma mark - *******懒加载*******
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.llz_width, self.view.llz_height) style:UITableViewStyleGrouped];
        _tableview.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.estimatedRowHeight = 200;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.backgroundColor = [UIColor whiteColor];
        //    [_tableview registerNib:[UINib nibWithNibName:@"FeedCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"FeedCell"];
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

- (NSMutableArray *)testData{
    if (!_testData) {
        //添加测试数据
        _testData = [[NSMutableArray alloc]init];
        _testData = [self addTestData];;
    }
    return _testData;
}

#pragma mark - *******生命期*******
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我关注的";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealPostNotification:) name:@"postNotification" object:nil];
    
}

#pragma mark - *******tableview DataSource*******
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    FeedCell *feedCell = [tableView dequeueReusableCellWithIdentifier:@"FeedCell" forIndexPath:indexPath];
    FeedCell *feedCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!feedCell) {
        feedCell = [[[NSBundle mainBundle]loadNibNamed:@"FeedCell" owner:self options:nil]firstObject];
    }
    [self configureCell:feedCell atIndexPath:indexPath];
    return feedCell;//返回个人信息cell
}

- (void)configureCell:(FeedCell *)cell atIndexPath:(NSIndexPath *)indexPath {
//    cell.fd_enforceFrameLayout = YES;
    cell.delegate = self;
    //数据刷新
    FeedModel *model = self.testData[indexPath.section];
    cell.model = model;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.testData.count;
}

#pragma mark - *******tableview Delegate*******
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return [tableView fd_heightForCellWithIdentifier:@"FeedCell" configuration:^(FeedCell *feedCell) {
//        [self configureCell:feedCell atIndexPath:indexPath];
//    }];
//
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    OneFeedViewController *one = [[OneFeedViewController alloc]init];
    one.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:one animated:YES];
}

#pragma mark - *******feedDelegate*******
///关注按钮被点击
- (void)followBtnClicked:(FeedCell *)feedCell button:(UIButton *)followBtn{
    if([followBtn.titleLabel.text isEqualToString:@"关注"]){
        [followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
    }else{
        [followBtn setTitle:@"关注" forState:UIControlStateNormal];
    }
}

///价值按钮被点击
- (void)moneyBtnClicked:(FeedCell *)feedCell view:(UIView *)view{
    [SVProgressHUD showInfoWithStatus:@"预计一周后的收入"];
    [SVProgressHUD dismissWithDelay:0.5];
}

///点赞按钮被点击
- (void)thumbBtnClicked:(FeedCell *)feedCell view:(UIView *)view{
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UIImageView class]]) {
            UIImageView *imageView = (UIImageView *)subview;
            if ([imageView.image isEqual:[UIImage imageNamed:@"good"]]) {
                imageView.image = [UIImage imageNamed:@"good-filling"];
            }else if ([imageView.image isEqual:[UIImage imageNamed:@"good-filling"]]) {
                [SVProgressHUD showErrorWithStatus:@"请勿重复点赞"];
                [SVProgressHUD dismissWithDelay:0.5];
                break;
            }
        }
        if ([subview isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)subview;
            [label setText:[NSString stringWithFormat:@"%ld",(label.text.integerValue + 1)]];
            //点赞之后，价值增加
            UILabel *moneyLabel = feedCell.money;
            [moneyLabel setText:[NSString stringWithFormat:@"%ld",moneyLabel.text.integerValue + 7]];
        }

    }
}

///评论按钮被点击
- (void)commentBtnClicked:(FeedCell *)feedCell view:(UIView *)view{
    OneFeedViewController *one = [[OneFeedViewController alloc]init];
    one.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:one animated:YES];
}

///收藏按钮被点击
- (void)collectionBtnClicked:(FeedCell *)feedCell button:(UIButton *)collcetionBtn{
    collcetionBtn.selected = collcetionBtn.selected ? NO : YES;
    [collcetionBtn setImage:[UIImage imageNamed:@"favorites-filling.png"] forState:UIControlStateSelected];
    if (collcetionBtn.selected) {
        [SVProgressHUD showSuccessWithStatus:@"成功收藏"];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"取消收藏"];
    }
    [SVProgressHUD dismissWithDelay:0.5];
}

///头像被点击
- (void)avatorClicked:(NSString *)accountID{
    VisitViewController *visit = [[VisitViewController alloc]init];
    visit.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:visit animated:YES];
}

///预览图片
- (void)photosClicked:(NSArray *)photos{
    NSMutableArray <UIImage *>* images = [[NSMutableArray alloc]init];
    for (NSString *name in photos){
        UIImage *image = [UIImage imageNamed:name];
        [images addObject:image];
    }
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:images currentImageIndex:0];
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:@"取消" deleteButtonTitle:nil otherButtonTitles:@"保存图片",nil];
    
}

///长按手势回调
- (void)report{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"举报" message:@"请选择举报的类型" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"违法内容" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD showSuccessWithStatus:@"举报成功"];
        [SVProgressHUD dismissWithDelay:0.5];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"垃圾营销" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD showSuccessWithStatus:@"举报成功"];
        [SVProgressHUD dismissWithDelay:0.5];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"侵权行为" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD showSuccessWithStatus:@"举报成功"];
        [SVProgressHUD dismissWithDelay:0.5];
    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - *******XLPhotoBrowserDelegate*******
- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex{
    
    switch (actionSheetindex) {
        case 0: {[browser saveCurrentShowImage];}// 保存
            break;
        default:
            break;
    }
}

#pragma mark - *******添加测试数据*******
- (NSMutableArray *)addTestData{
    NSMutableArray *Data = [[NSMutableArray alloc]init];
    for (int i = 1; i < 6; i++) {
        NSDictionary *dic = [NSDictionary dictionary];
    
        switch (i) {
            case 1:
                 dic = @{@"imageName":@"avator1.png",
                         @"name":@"胖虎",
                         @"devote":@"30",
                         @"photos":@[@"风景13",@"风景14",@"风景15",@"风景16"],
                          @"postContent":@"西湖的音乐喷泉很不错哦！",
                          @"location":@"杭州市",
                          @"time":@"1小时前",
                          @"moneyNum":@"23",
                          @"thumbNum":@"97",
                          @"commentNum":@"4",
                         @"tags":@"风景",
                  };
                break;
            case 2:
                dic = @{@"imageName":@"avator2.png",
                        @"name":@"叮当猫",
                        @"devote":@"42",
                        @"photos":@[@"美食1",@"美食2",@"美食3",@"美食4"],
                        @"postContent":@"肚子好饿，我的口袋去哪里了？？？",
                        @"location":@"宁波市",
                        @"time":@"7小时前",
                        @"moneyNum":@"53",
                        @"thumbNum":@"109",
                        @"commentNum":@"10",
                        @"tags":@"美食",
                        };
                break;
            case 3:
                dic = @{@"imageName":@"avator3.png",
                        @"name":@"哪吒",
                        @"devote":@"47",
                        @"photos":@[@"风景1",@"风景2",@"风景3",@"风景4",@"风景5",@"风景6",@"风景7",@"风景8",@"风景9"],
                        @"postContent":@"好想去这些地方！！！",
                        @"location":@"北京天安门广场",
                        @"time":@"昨天",
                        @"moneyNum":@"93",
                        @"thumbNum":@"199",
                        @"commentNum":@"29",
                        @"tags":@"风景",
                        };
                break;
            case 4:
                dic = @{@"imageName":@"avator4.png",
                        @"name":@"球球",
                        @"devote":@"27",
                        @"photos":@[@"军事1",@"军事2",@"军事3",@"军事4",@"军事5"],
                        @"postContent":@"国家越来越强大了。",
                        @"location":@"杭州市西溪湿地",
                        @"time":@"三天前",
                        @"moneyNum":@"153",
                        @"thumbNum":@"309",
                        @"commentNum":@"98",
                        @"tags":@"军事",
                        };
                break;
            case 5:
                dic = @{@"imageName":@"avator5.png",
                        @"name":@"可达鸭",
                        @"devote":@"79",
                        @"photos":@[@"萌宠1",@"萌宠2",@"萌宠3",@"萌宠4",@"萌宠5",@"萌宠6"],
                        @"postContent":@"好可爱",
                        @"location":@"海口市",
                        @"time":@"2018-3-29 14:21",
                        @"moneyNum":@"66",
                        @"thumbNum":@"201",
                        @"commentNum":@"101",
                        @"tags":@"萌宠",
                        };
                break;
            default:
                break;
        }

        FeedModel *model = [[FeedModel alloc]initWithFeedModel:dic];
        [Data addObject:model];
    }
    return Data;
}

#pragma mark - *******布局*******
- (void)viewDidLayoutSubviews{
    self.tableview.frame = self.view.bounds;
}

#pragma mark - *******私有方法*******
- (void)dealPostNotification:(NSNotification *)noti{
    FeedModel *model = noti.object;
    [self.testData insertObject:model atIndex:0];
    [self.tableview reloadData];
}

@end
